﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.LanguageWire.LWIntegration;
using iLangl.Model;

namespace iLangl.LanguageWire.DAL.LanguageWireDAL.RealStorage
{
	public class InvoiceDAL : BaseStorageLWAPI, IInvoiceDAL
	{
		public void CreateInvoice(BillingInfo billingInfo)
		{
			
				// If object already registered at LW side we skip registration
				if (!String.IsNullOrEmpty(billingInfo.TPIdentity))
					return;
			

			CreateInvoicingAccountRequest createInvoiceRequest = new CreateInvoicingAccountRequest();


			createInvoiceRequest.AuthKey = LanguageWireConfiguration.Instance.AuthKey;

			//Define LW Company Id
			createInvoiceRequest.CompanyId = int.Parse(billingInfo.Company.TPIdentity);
			createInvoiceRequest.CurrencyId = LanguageWireConfiguration.Instance.CurrencyId;
			if (createInvoiceRequest.BillingContactInformation == null)
				createInvoiceRequest.BillingContactInformation = new InvoicingContactInformation();

			BillingInfo bi = null;

			if (billingInfo == null)
			{
				createInvoiceRequest.Name = "Default Account";
				createInvoiceRequest.BillingContactInformation.CountryId = LanguageWireConfiguration.Instance.BillingCountryId;
				createInvoiceRequest.BillingContactInformation.City = LanguageWireConfiguration.Instance.BillingCity;
				createInvoiceRequest.BillingContactInformation.Email = LanguageWireConfiguration.Instance.BillingEmail;
				createInvoiceRequest.BillingContactInformation.FirstName = LanguageWireConfiguration.Instance.BillingFirstName;
				createInvoiceRequest.BillingContactInformation.LastName = LanguageWireConfiguration.Instance.BillingLastName;
				createInvoiceRequest.BillingContactInformation.PostalCode = LanguageWireConfiguration.Instance.BillingPostalCode;
				createInvoiceRequest.BillingContactInformation.StreetAddress = LanguageWireConfiguration.Instance.BillingStreetAddress;
				createInvoiceRequest.BillingContactInformation.StreetAddress2 = String.Empty;
				createInvoiceRequest.BillingContactInformation.CompanyName = LanguageWireConfiguration.Instance.BillingCompanyName;
			}
			else
			{
				bi = billingInfo;

				if (string.IsNullOrEmpty(bi.Name))
				{
					createInvoiceRequest.Name = billingInfo.Company.Name;
					createInvoiceRequest.BillingContactInformation.CompanyName = billingInfo.Company.Name;
				}
				else
				{
					createInvoiceRequest.Name = bi.Name;
					createInvoiceRequest.BillingContactInformation.CompanyName = bi.Name;
				}

				createInvoiceRequest.BillingContactInformation.CountryId = LanguageWireConfiguration.Instance.BillingCountryId;//Country Mapping
				createInvoiceRequest.BillingContactInformation.City = bi.BillingCity;
				createInvoiceRequest.BillingContactInformation.Email = bi.BillingEmail;
				createInvoiceRequest.BillingContactInformation.FirstName = bi.BillingFirstName;
				createInvoiceRequest.BillingContactInformation.LastName = bi.BillingLastName;
				createInvoiceRequest.BillingContactInformation.PostalCode = bi.BillingPostalCode;
				createInvoiceRequest.BillingContactInformation.StreetAddress = bi.BillingStreetAddress;
				createInvoiceRequest.BillingContactInformation.StreetAddress2 = bi.BillingStreetAddress2;


			}

			ClientManagementServiceClient client = new ClientManagementServiceClient();
			int invoiceAccountId = client.CreateInvoicingAccount(createInvoiceRequest);

			if (bi != null)
			{
				bi.TPIdentity = invoiceAccountId.ToString();
				iLangl.LanguageWire.DAL.iLanglDAL.DALFacade.Instance.CompanyDAL.UpdateBillingInfoReference(bi.Id, invoiceAccountId, null);
			}
		}
	}
}
